package com.etc.zzsys.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.zzsys.entity.Question;
import com.etc.zzsys.entity.User;
import com.etc.zzsys.service.QuestionService;
import com.etc.zzsys.service.UserService;
import com.etc.zzsys.service.impl.QuestionServiceimpl;
import com.etc.zzsys.service.impl.UserServiceImpl;
import com.etc.zzsys.util.PageData;

/**
 * Servlet implementation class QuestionServlet
 */
@WebServlet("/qs")
public class QuestionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      private QuestionService qs=new QuestionServiceimpl();
      private UserService us=new UserServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuestionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("utf-8");
		
		String op=request.getParameter("op");
		if("querylist".equals(op)) {
//			List<Question> qList=qs.getQuestions();
//			request.setAttribute("qList", qList);
//			List<User> ulist=us.getUsers();
//			request.setAttribute("ulist", ulist);
//			request.getRequestDispatcher("questionList.jsp").forward(request, response);
			doQuery(request, response);
		}
		
		if("add".equals(op)) {
			List<User> ulist=us.getUsers();
			request.setAttribute("ulist", ulist);
			request.getRequestDispatcher("addQuestion.jsp").forward(request, response);
		}
		
		if("doAdd".equals(op)) {
			String title=request.getParameter("title");
			String content=request.getParameter("content");
			SimpleDateFormat simple=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String qdate=simple.format(new Date());
			int userId=Integer.valueOf(request.getParameter("userId"));
			
			Question q=new Question();
			q.setTitle(title);
			q.setContent(content);
			q.setQdate(qdate);
			boolean result=qs.addQuestion(q, userId);
			if(result) {
				request.getRequestDispatcher("qs?op=querylist").forward(request, response);
			}
		}
		
		if("del".equals(op)) {
			int qId=Integer.valueOf(request.getParameter("qId"));
			boolean result=qs.delQuestion(qId);
			if(result) {
				request.getRequestDispatcher("qs?op=querylist").forward(request, response);
			}
		}
		if("updateDetail".equals(op)) {
			int id=Integer.valueOf(request.getParameter("id"));
			List<User> ulist=us.getUsers();
			request.setAttribute("ulist", ulist);
			
			Question q=(Question)qs.getQuestionById(id);
			request.setAttribute("question", q);
			request.getRequestDispatcher("updateQuestion.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void doQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		QuestionService qs=new QuestionServiceimpl();
		
		int page=1;
		int pageSize=10;
		String key="";
		if(request.getParameter("page")!=null) {
			page=Integer.valueOf(request.getParameter("page"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize=Integer.valueOf(request.getParameter("pageSize"));
		}
		if(request.getParameter("key")!=null) {
			key=request.getParameter("key");
		}
		if(request.getParameter("key")!=null) {
			key=request.getParameter("key");
		}
		request.setAttribute("key",key);
		PageData<Question> pdata=qs.queryQuestionByPage(page, pageSize, key);
		List<User> ulist=us.getUsers();
		request.setAttribute("ulist", ulist);
		request.setAttribute("pdata", pdata);
		request.getRequestDispatcher("questionList.jsp").forward(request, response);
	}
}
